<?php
namespace app\serverintegralmall\service\goods;
use \app\manage\service\ApiBase;
/**
 * @author:      李颖
 * @DateTime:    2017-10-31 10:17:50
 * @Description: 积分商城 - 商品
 */
class Goods extends ApiBase{
	

	protected $goodsLogic = '\app\serverintegralmall\logic\Goods';
	protected $goodsCateLogic = '\app\serverintegralmall\logic\GoodsCategory';
    protected $goodsValidate = '\app\serverintegralmall\validate\Goods';

    /*艺货后台发布/变价积分商品时，对基本数据以及分类的验证*/
    protected function _checkData($data, $scene = ''): array 
    {
    	$validate = validate($this->goodsValidate);
        if(!$validate->scene($scene)->check($data)){  return verify_err(null, $validate->getError());  }
        if(isset($data['cate_id'])){
        	$checkCate = logic($this->goodsCateLogic)->checkReadCate(['id'=>$data['cate_id'], 'app_id'=>$data['app_id']]);
        	if(!$checkCate){  return verify_err(null, '该商品分类的状态错误');  } 
        }
        return auto_success($data, '检验数据正常');
    }

    /*艺货后台发布积分商品*/
    public function addIntegralGoods(): array 
    {
    	$data = $this->haveIssetReceiveData(['app_id'=>0,'user_id'=>0, 'cate_id'=>0, 'title'=>' ', 'integral'=>0, 'product_code'=>' ', 'profile_pic'=>'', 'imgs_urls'=>' ', 'imgs_ids'=>' ', 'origin_number'=>0, 'order'=>0, 'desc'=>' ']);

    	$checkData = $this->_checkData($data);
    	if($checkData['status'] != SUCCESS_STATUS){ return $checkData; }

    	return $this->logic($this->goodsLogic)->addIntegralGoods($data);
    }

    /*后台修改积分商品*/
    public function updateIntegralGoods(): array 
    {
    	$data = $this->issetReceiveData(['id'=>0, 'app_id'=>0, 'cate_id'=>0, 'title'=>' ', 'integral'=>0, 'profile_pic'=>'', 'imgs_urls'=>' ', 'imgs_ids'=>' ', 'number'=>0, 'origin_number'=>0, 'order'=>0, 'desc'=>' ']);

        $checkData = $this->_checkData($data);
        if($checkData['status'] != SUCCESS_STATUS){ return $checkData; }

        return $this->logic($this->goodsLogic)->updateIntegralGoods($data);
    }

    /*艺货后台获取积分商品分页列表*/
    public function allGoodsList(): array 
    {
        $data = $this->haveIssetReceiveData(['app_id'=>0, 'keywords'=>'', 'page'=>1, 'listRows'=>15]);
        return $this->stepRun($data, 'allGoodsList');
    }

    /*管理员下架某一个商品*/
    public function soldOutOneGoods(): array 
    {
        $data = $this->haveIssetReceiveData(['id'=>0,'app_id'=>0]);
        $data['id'] = [$data['id']];
        $data['status'] = 3;
        return $this->_adminBatchOperaGoods($data, 'manageBatchSoldOutGoods');
    }

    /*管理员下架某一批商品*/
    public function soldOutBatchGoods(): array 
    {
        $data = $this->haveIssetReceiveData(['id'=>0,'app_id'=>0]);
        $data['status'] = 3;
        return $this->_adminBatchOperaGoods($data, 'manageBatchSoldOutGoods');
    }

    /*管理员批量操作（下架）商品,不包含删除*/
    protected function _adminBatchOperaGoods($data, $operate): array 
    {

        $validate = validate($this->goodsValidate);
        if(!$validate->scene($operate)->check($data)){
            return verify_err(null, $validate->getError());
        }

        $checkGoodsRes = $this->_adminBatchCheckGoods($data, $operate);
        if($checkGoodsRes['status'] != SUCCESS_STATUS){
            return $checkGoodsRes;
        }

        return $this->logic($this->goodsLogic)->batchOperaGoods($data, $operate);
    }

    /*管理员批量操作商品时，对商品批量检验是否可被操作 $data['id'] = [1,3] */
    protected function _adminBatchCheckGoods(Array $data, $operate = ''): array 
    {
        $goodsList = $this->logic($this->goodsLogic)->dbGoodsList(['id'=>$data['id']]);
        if(count($goodsList) == 0){
            return verify_err(null,'没有要操作的商品');
        }
        if(count($goodsList) != count($data['id'])){
            return verify_err(null,'其中包含不可执行该操作的商品');
        }
        foreach ($goodsList as $k => $v) {
            if($operate == 'manageBatchSoldOutGoods' && !in_array($v['status'], ['1','4'])){
                return verify_err(null,'失败！标题为'.$v['title'].'的商品不可下架');
            }
        }
        return auto_success(null,'该批商品可被操作');
    }

    /*管理员删除商品*/
    public function manageDelGoods(): array 
    {
        $data = $this->haveIssetReceiveData(['id'=>0,'app_id'=>0]);
        return $this->stepRun($data,'manageDelGoods','manageOperaGoods');
    }

    /*管理员批量删除商品*/
    public function manageBatchDelGoods(): array 
    {
        $data = $this->haveIssetReceiveData(['id'=>[],'app_id'=>0]);
        return $this->stepRun($data,'manageDelGoods','manageBatchOperaGoods');
    }

    /*前台获取积分商品分页列表*/
    public function frontGoodsList(): array 
    {
        $data = $this->haveIssetReceiveData(['app_id'=>0, 'page'=>1, 'listRows'=>15, 'keywords'=>'']);
        return $this->stepRun($data, 'frontGoodsList');
    }

    /*前台获取某积分商品的详情*/
    public function frontGoodsInfo(): array 
    {
        $data = $this->haveIssetReceiveData(['app_id'=>0, 'id'=>0]);
        return $this->stepRun($data, 'frontGoodsInfo');
    }










}